package com.gnerv.battle.boot.module.base.business.impl;

import com.gnerv.battle.boot.module.base.model.bo.DictBO;
import com.gnerv.battle.boot.module.base.model.dto.DictDetailsDTO;
import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.module.base.business.DictBusiness;
import com.gnerv.battle.boot.module.base.model.dto.DictCreateDTO;
import com.gnerv.battle.boot.module.base.model.dto.DictDTO;
import com.gnerv.battle.boot.module.base.model.dto.DictUpdateDTO;
import com.gnerv.battle.boot.module.base.service.DictItemService;
import com.gnerv.battle.boot.module.base.service.DictService;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class DictBusinessImpl implements DictBusiness {

    private final DictService dictService;

    private final DictItemService dictItemService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(DictCreateDTO dictCreateDTO) {
        DictBO dictBO = dictCreateDTO.toDictBO();
        dictBO.setUkId(UUIDTools.lower());
        dictService.create(dictBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        dictService.deleteByUkId(ukId);
        dictItemService.deleteByDictUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(DictUpdateDTO dictUpdateDTO) {
        dictService.updateByUkId(dictUpdateDTO.toDictBO());
        return RestResult.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<DictDTO> selectByUkId(String ukId) {
        return RestResult.ok(dictService.selectByUkId(ukId));
    }

    @Override
    public RestResult<DictDetailsDTO> selectDetailsByCode(String code) {
        return RestResult.ok(dictService.selectDetailsByCode(code));
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<DictDTO>> listByName(String name) {
        return RestResult.ok(dictService.listByName(name));
    }

    @Override
    public RestResult<List<DictDetailsDTO>> listDetailsByName(String name) {
        return RestResult.ok(dictService.listDetailsByName(name));
    }

}
